﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_KhenThuong : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_KhenThuong()
        {
            InitializeComponent();
        }
        KetNoi_KhenThuong kn=new KetNoi_KhenThuong();
        public void LoadDuLieu()
        {

            gridControl1.DataSource = kn.Load_KhenThuong();
        }
        public void Nut()
        {
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            btnHuy.Enabled = true;
        }
        private void uc_KhenThuong_Load(object sender, EventArgs e)
        {
            kn.connect();
            lbMaKhenThuong.Hide();
            txtMaKhenThuong.Hide();
            LoadDuLieu();
            btnHuy.Enabled = false;
            btnLuu.Enabled = false;

        }
        int flag;
        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 0;
            Nut();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Nut();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            kn.xoa_KhenThuong(txtMaKhenThuong.Text);
            LoadDuLieu();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if(flag==0)
            {
                kn.them_KhenThuong(txtTenKhenThuong.Text,date_NgayQuyetDinh.Text ,txtMaNhanVien.Text ,txtNoiDung.Text ,txtHinhThuc.Text);
            }
            if(flag==1)
            {
                kn.sua_KhenThuong(txtMaKhenThuong.Text ,txtTenKhenThuong.Text,date_NgayQuyetDinh.Text ,txtMaNhanVien.Text ,txtNoiDung.Text ,txtHinhThuc.Text);
            }
            LoadDuLieu();
  
            btnThem.Enabled =true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;

            btnLuu.Enabled = false;
            btnHuy.Enabled = false;
        
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaKhenThuong.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Loại Khen Thưởng").ToString();
            txtTenKhenThuong.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Tên Loại Khen Thưởng").ToString();
            date_NgayQuyetDinh.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Ngày Quyết Định").ToString();
            txtMaNhanVien.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhân Viên").ToString();
            txtNoiDung.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Nội Dung").ToString();
            txtHinhThuc.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Hình Thức").ToString();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void txtMaNhanVien_TextChanged(object sender, EventArgs e)
        {
            string s = "select * from tbl_NhanVien where MaNV='" + txtMaNhanVien.Text + "'";
            DataTable dt = new DataTable();
            try
            {

                dt = kn.taobang(s);
            }
            catch (Exception)
            {
               
            }
            if (dt.Rows.Count != 0)
                errorProvider1.Clear();
            else
            {
                errorProvider1.SetError(txtMaNhanVien, "Mã Nhân Viên Không Tồn Tại!");
                txtMaNhanVien.Focus();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {

        }
    }
}
